// router.js
import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'

import Login from '../views/login/'
Vue.use(Router)
/* Layout */
import Layout from '@/layout'
const dashboard = resolve => require(['../views/dashboard/index'], resolve)

// 所有权限通用路由表
// 如首页和登录页和一些不用权限的公用页面
export const constantRouterMap = [
  { path: '/login', component: Login },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '主页', icon: 'dashboard' }
    },{
      path: 'updatePwd',
      name: 'updatePwd',
      component: () => import('@/views/dashboard/updatePwd'),
      meta: { title: '修改密码' },
      hidden: true
    },{
      path: 'notification',
      name: 'notification',
      component: () => import('@/views/dashboard/notification'),
      meta: { title: '消息通知' },
      hidden: true
    },]
  }
]
// 异步挂载的路由
// 动态需要根据权限加载的路由表
export const asyncRouterMap = [
  {
    path: '/info',
    redirect: '/info/infoManage',
    component: Layout,
    meta: { title: '信息管理', icon: 'el-icon-user-solid', role: ['user', 'support']},
    children: [
      {
        path: 'infoManage',
        name: 'InfoManage',
        component: () => store.getters.role == 'user' ? import('@/views/user/infoManage') 
        : import('@/views/support/infoManage'),
        meta: { title: '个人信息', icon: 'form', role: ['user', 'support']}
      },
      {
        path: 'userReview',
        name: 'UserReview',
        component: () => import('@/views/support/userReview'),
        meta: { title: '用户审核', icon: 'table', role: ['support']}
      }
    ]
  },
  {
    path: '/orderSystem',
    component: Layout,
    name: 'OrderSystem',
    meta: { title: '工单系统', icon: 'el-icon-s-help', role: ['user', 'support']},
    children: [
      {
        path: 'orderProcess',
        name: 'orderProcess',
        component: () => store.getters.role == 'user' ? import('@/views/user/askQuestion') 
        : import('@/views/support/answerQuestion'),
        meta: { title: '工单处理', icon: 'form', role: ['user', 'support']}
      },
      {
        path: 'ordering',
        name: 'Ordering',
        component: () => import('@/views/user/ordering'),
        meta: { title: '进行中工单', icon: 'table', role: ['user']}
      },
      {
        path: 'pendingOrder',
        name: 'PendingOrder',
        component: () => import('@/views/support/pendingOrder'),
        meta: { title: '待办工单', icon: 'table', role: ['support']}
      },
      {
        path: 'followOrder',
        name: 'FollowOrder',
        component: () => import('@/views/support/followOrder'),
        meta: { title: '跟进工单', icon: 'table', role: ['support']}
      },      
      {
        path: 'orderList',
        name: 'OrderList',
        component: () => import('@/views/user/orderList'),
        meta: { title: '工单列表', icon: 'table', role: ['user']}
      },
    ]
  },
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router

